<template>
  <el-dialog
    :visible.sync="isShow"
    :rules="rules"
    width="600px"
    center
    :title="{ add: '新增企业', edit: '编辑企业' }[mode]"
  >
    <el-form :model="form" :rules="rules" label-width="120px" ref="form">
      <el-form-item prop="eid" label="企业编号">
        <el-input v-model="form.eid"></el-input>
      </el-form-item>
      <el-form-item prop="name" label="企业名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item prop="short_name" label="企业简称">
        <el-input v-model="form.short_name"></el-input>
      </el-form-item>
      <el-form-item prop="intro" label="企业简介">
        <el-input v-model="form.intro"></el-input>
      </el-form-item>
      <el-form-item prop="remark" label="备注">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="footer">
      <el-button @click="isShow = false">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-dialog>
</template>
5
<script>
import { enterpriseAdd, enterpriseEdit } from '@/api/business.js'
export default {
  props: ['modeSon'],
  watch: {
    isShow (newVal) {
      if (newVal === false) {
        this.form = {
          eid: '',
          name: '',
          short_name: '',
          intro: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      }
    }
  },
  data () {
    return {
      isShow: false,
      form: {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      rules: {
        eid: [{ required: true, message: '请输入企业编号', trigger: 'change' }],
        name: [
          { required: true, message: '请输入企业名称', trigger: 'change' }
        ],
        short_name: [
          { required: true, message: '请输入企业简称', trigger: 'change' }
        ],
        intro: [
          { required: true, message: '请输入企业简介', trigger: 'change' }
        ]
      },
      mode: 'add'
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate((result) => {
        if (result) {
          if (this.modeSon === 'edit') {
            enterpriseEdit(this.form).then(() => {
              this.$message.success('编辑成功')
              this.isShow = false
              this.$parent.search()
            })
          } else {
            enterpriseAdd(this.form).then(() => {
              this.$message.success('添加成功')
              this.isShow = false
              this.$parent.search()
            })
          }
        } else {
          this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__header {
  background: linear-gradient(to right, #02c3fa, #1299fa);

  .el-icon-close:before {
    color: #fff;
  }

  .el-dialog__title {
    color: #fff;
  }

  .footer {
    text-align: center;
  }
}
</style>
